package com.fulu.game.bi.service.impl.play;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.common.util.lang.StringUtils;
import com.fulu.game.bi.entity.bo.channel.GameGuildStatListBO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.play.GameGuildStatVO;
import com.fulu.game.bi.hbase.HBaseGameGuildStatDAO;
import com.fulu.game.bi.redis.service.play.GameGuildService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GameGuildServiceImpl extends BasePlayService implements GameGuildService {

    @Autowired
    private HBaseGameGuildStatDAO hBaseGameGuildStatDAO;

    @Override
    public PageInfo<GameGuildStatVO> list(GameGuildStatListBO listBO) {
        listBO.setTableName(gameGuildStat.get(listBO.getQueryType()));
        listBO.setPageNum((listBO.getPageNum() - 1) * listBO.getPageSize());
        listBO.setGuildName(StringUtils.isBlank(listBO.getGuildName()) ? listBO.getGuildName() : "%" + listBO.getGuildName() + "%");
        List<GameGuildStatVO> list = hBaseGameGuildStatDAO.list(listBO);
        Integer count = hBaseGameGuildStatDAO.count(listBO);
        list = list.stream().map(map -> {
            map.setQueryType(listBO.getQueryType());
            return map;
        }).collect(Collectors.toList());
        PageInfo<GameGuildStatVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        return pageInfo;
    }

    @Override
    public void export(GameGuildStatListBO bo) {
        String fileName = "陪玩公会";
        LinkedHashMap<String, Mapper<GameGuildStatVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(bo.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("公会ID", v -> v.getGuildCode());
        map.put("公会名称", v -> v.getGuildName());
        map.put("皮皮币流水", v -> v.getPipiMoneyAll().toString());
        map.put("魅力值流水", v -> v.getCharmMoneyAll().toString());
        map.put("新增陪玩师", v -> v.getNewAddPlayeeNum() + "");
        map.put("活跃陪玩师", v -> v.getLivelyPlayeeNum() + "");
        map.put("接单陪玩师", v -> v.getOrderPlayeeNum() + "");
        map.put("退款陪玩师", v -> v.getRefundPlayeeNum() + "");
        map.put("下单人数", v -> v.getOrderUserNum() + "");
        map.put("下单次数", v -> v.getOrderNum() + "");
        map.put("下单金额", v -> v.getOrderMoneyAll().toString());
        map.put("下单时长", v -> v.getOrderDurationTime().toString());
        map.put("退款人数", v -> v.getRefundUserNum() + "");
        map.put("退款次数", v -> v.getRefundNum() + "");
        map.put("打赏人数", v -> v.getRewardUserNum() + "");
        map.put("打赏次数", v -> v.getRewardNum() + "");
        map.put("打赏金额", v -> v.getRewardMoneyAll().toString());
        Querier querier = (num) -> list(bo);
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
